package han.study.transaction;

import han.study.bean.User;
import han.study.utils.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Objects;

public class TransactionTest {

    // 通用增删改操作 ---version1.0
    public int updateTest(String sql, Object... args){
        Connection conn = null;
        PreparedStatement ps = null;
        try{
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            return ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,ps);
        }
        return 0;
    }
    // 考虑数据事务的情况
    public int update(Connection conn,String sql,Object... args){
        PreparedStatement ps = null;
        try{
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            return ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(null,ps);
        }
        return 0;
    }
    // 数据库单个查询;考虑了事务
    public <T> T getInstance(Connection conn, Class<T> clazz, String sql, Object... args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            if(rs.next()){
                T t = clazz.getDeclaredConstructor().newInstance();
                for(int i=1;i<=columnCount;i++){
                    String columnLabel = metaData.getColumnLabel(i);
                    Object columnValue = rs.getObject(i);
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columnValue);
                }
                return t;
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(null,ps,rs);
        }
        return null;
    }
    @Test
    public void testUpdate(){
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "update user_table set balance = balance - 100 where user = ?";
            update(conn,sql,"AA");
            String sql2 = "update user_table set balance = balance + 100 where user = ?";
            update(conn,sql2,"BB");
            conn.commit();
            System.out.println("转账成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
    }
    @Test
    public void testTransactionSelect() throws Exception{
        Connection conn = JDBCUtils.getConnection();
        String sql = "select user,password,balance from user_table where user=?";
        User user = getInstance(conn,User.class,sql,"AA");
        System.out.println(user);
        JDBCUtils.closeResource(conn,null);
    }

}
